package cn.edu.nuc.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.common.Parameter;
import cn.edu.nuc.film.common.UnitHelper;
import cn.edu.nuc.film.dao.FilmMapper;
import cn.edu.nuc.film.dao.FilmTypeMapper;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmType;
import cn.edu.nuc.film.service.interfaces.FilmTypeService;

@Service
public class FilmTypeServiceImpl implements FilmTypeService {
	
	@Autowired
	private FilmTypeMapper filmTypeMapper;
	@Autowired
	private FilmMapper filmMapper;
	
	
	/**
	 * 获取类型列表，传值到addFilm前台
	 * @return   List<FilmType>
	 */
	public List<FilmType> typelist(){
		return filmTypeMapper.typeList();
	}
	
	
	
	/**
	 * 影片类型列表
	 */
	public Pager<FilmType> filmTypeList( PageControl pc,String name) {
		
		Parameter para = new Parameter();
		para.setStart( (pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset( pc.getPagesize());
		para.setName(name);
		//获取每页的具体数据
		List<FilmType> list = filmTypeMapper.filmTypeList( para );
		//设置总记录数
		int pagecount = 0;
		if( name != null ){
			pagecount = list.size();
		}else{
			pagecount = filmTypeMapper.count();
		}
		//测试是否获得
		/*System.out.println("-----------------filmlist------------------");
		System.out.println(list);*/
		Pager<FilmType> pager = UnitHelper.execlist(pagecount, list, pc);
		return pager;
		
	}
	
	
	/**
	 * 添加分类
	 */
	public int insert(FilmType filmType) {
		
		int count = filmTypeMapper.insert(filmType);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("添加失败");
		}
		
	}

	/**
	 * 获取影片类型的详细信息
	 */
	public FilmType detailFtId(int ftId) {
		FilmType filmType = filmTypeMapper.detailFtId(ftId);
		if( filmType != null ){
			return filmType;
		}else{
			throw new RuntimeException("查询失败");
		}
		
	}

	/**
	 * 编辑影片
	 */
	public int editFilmType(FilmType filmType) {
		int count = filmTypeMapper.updateByFtid(filmType);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("编辑失败");
		}
		
	}

	/**
	 * 查出ftId类型的电影
	 */
	public List<Film> searchFilmByFtid(int ftId) {
		List<Film> filmlist = filmMapper.searchFilmByFtid(ftId);
		return filmlist;
	}

	

}
